<template>
  <div>
    <el-dialog
      :title="this.$t('common.Add')"
      :visible.sync="Visible"
      width="500px"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div style="border-bottom:0;" class="midContant clearfix">
        <span style="text-align: right" class="Span borderR borderB"
          >{{$t('ReservoirRegionLang.str_num')}}:</span
        >
        <span class="Span borderB contant">
          <el-select
            ref="storage_area_code"
            class="Required"
            filterable
            @blur="handleBlur('storage_area_code', $event)"
            @change="handleChang('storage_area_code')"
            v-model="data.storage_area_code"
            :placeholder="$t('common.select_plactholder')"
          >
            <el-option
              v-for="item in options_storage"
              :key="item.id"
              :label="item.name_current"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </span>
        <span style="text-align: right" class="Span borderR borderB"
          >{{$t('ReservoirRegionLang.str_num')}}:</span
        >
        <span class="Span borderB contant">
          <el-input-number
            controls-position="right"
            class="Required"
            size="small" 
            ref="start_number" 
            @blur="handleBlur('start_number', $event)" 
            @focus="handleFocus($event)" 
            :min="1" 
            v-model="data.start_number">
          </el-input-number>
        </span>
        <span style="text-align: right" class="Span borderR borderB"
          >{{$t('ReservoirRegionLang.generate_num')}}:</span
        >
        <span class="Span borderB contant">
          <el-input-number
            controls-position="right"
            class="Required"
            size="small" 
            ref="end_number" 
            @blur="handleBlur('end_number', $event)" 
            @focus="handleFocus($event)" 
            :min="1" 
            v-model="data.end_number">
          </el-input-number>
        </span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm" size="small"
          >{{$t('common.confirm')}}</el-button
        >
        <el-button @click="cancel" size="small">{{$t('common.cancel')}}</el-button>
      </span>
      
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
  },
  data() {
    return {
      Visible: false,
      address_id:"",
      data: {
        storage_area_code:'',
        start_number:"",
        end_number:"",
      },
      options_storage:[],
      confirmTrue:true,//确定按钮保护
    };
  },
  created() {
    
  },
  mounted() {},
  watch: {
  },
  methods: {
    init(data) {
        this.address_id=data
        this.getOptions()
        this.Visible = true;
    },
    //获取下拉单数据
    getOptions(){
      //获取客户列表
      this.$get("getStorageArea",{address_id:this.address_id,status:0}).then((res) => {
        this.options_storage = res.data;
      });
    },
    // 提交
    confirm() {
      //是否有空值必选项标识
      let type = true;
      //输入框判断是否有值
      if (!this.data.storage_area_code&&this.data.storage_area_code!==0) {
        this.$refs.storage_area_code.$el.childNodes[1].childNodes[1].style.border =
          "1px solid red";
        type = false;
      }
      if(!this.data.start_number){
        this.$refs.start_number.$el.childNodes[2].childNodes[1].style.border='1px solid red'
        type=false
      }
      if(!this.data.end_number){
        this.$refs.end_number.$el.childNodes[2].childNodes[1].style.border='1px solid red'
        type=false
      }
      if (type && this.confirmTrue) {
        this.confirmTrue=false
        window.setTimeout(()=>{
          this.confirmTrue=true
        },1000);
        let params = new FormData();
        params.append("address_id", this.address_id);
        params.append("storage_area_code", this.data.storage_area_code);
        params.append("start_number", this.data.start_number);
        params.append("end_number", this.data.end_number);
        this.$postForm("addStorageAreaShelves", params).then((res) => {
          this.confirmTrue = true;
          if(res.success){
            this.$message.success(res.msg);
            this.$emit("success",this.levelType);
            this.cancel()
          }
        });
      }
    },
    //取消
    cancel() {
      this.data = {
        storage_area_code:"",
        start_number:"",
        end_number:"",
      };
      this.timeData=[],
      this.Visible = false;
      
      let dom = document.getElementsByClassName("el-input__inner");
      for (let i = 0; i < dom.length; i++) {
        dom[i].style.border = "1px solid #E0E3E9";
      }
    },
    //输入框、下拉框失去焦点边框变色
    handleBlur(name, event) {
      if (!this.data[name]&&this.data[name]!==0) {
        event.target.style.border = "1px solid red";
      }
    },
    //输入框、下拉框获取焦点边框变色
    handleFocus(event) {
      event.target.style.border = "1px solid #E0E3E9";
    },
    //下拉框有值后边框变色
    handleChang(data) {
      if (this.data[data].length==0) {
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border = "1px solid red";
      }else{
        this.$refs[data].$el.childNodes[1].childNodes[1].style.border ="1px solid #E0E3E9 ";
      }
    },
  },
};
</script>

<style scoped lang="less" >

.foot {
  /deep/.el-input--mini {
    width: 60% !important;
  }
}
.Span{
  width: 50%;
}


</style>